App.controller('todoMtController', ['$rootScope', '$scope', '$state', 'Page', function($rootScope, $scope, $state, Page) {
    Page.ajaxPost('employeeGroup/empTree.do', null, function(resp) {
        $scope.empList = resp.data;
    });
    $scope.grid = {
        view : function(row) {
            $scope.viewTask = row;
            Page.ajaxPost('maintenanceApply/fileList.do',{taskId : row.taskId},function(resp){
                $scope.viewTask.images = resp.data;
                $scope.modal.show();
            });
        },
        column_defs: [{
                target : 3,
                template : function(row) {
                    var html = '';
                    if(row.workGroupId != undefined && row.workGroupId != "") {
                        if(row.taskDetails != undefined && row.taskDetails.length > 0) {
                            for (var i = 0; i < row.taskDetails.length; i++) {
                                html = html + '<span style="margin-right: 7px;">' + row.taskDetails[i].empName + '</span>'
                            }
                        }
                    } else {
                        html = html +  '<span>' + row.empName + '</span>'
                    }
                    return html
                }

            },
            {
                target : 10,
                buttons : [
                    {
                        name: '查看',
                        class: 'btn-warning btm-sm',
                        icon: 'fa-edit',
                        event: 'view'
                    },
                    {
                        name: '办理',
                        class: 'btn-danger',
                        icon: 'glyphicon glyphicon-exclamation-sign',
                        event: function(row) {
                            $scope.selectEmpList = [];
                            $scope.completeTask = row;
                            if(row.workGroupId != undefined && row.workGroupId != "") { //针对工作组的情况
                                Page.ajaxPost('employeeGroup/getEmpListByGroupId.do',{'groupId' : row.workGroupId},function(resp){
                                    $scope.completeTask.empList = resp.data;
                                    $scope.hasOwnerFlag = false;
                                    $scope.completeModal.show();
                                });
                            } else if(row.owner != undefined && row.owner != "") { //针对有办理人
                                $scope.hasOwnerFlag = true;
                                $scope.completeModal.show();
                            }

                        },
                        visiable : function(row) {
                            if(row.status == '2') { //已完成
                                return false;
                            } else {
                                if(row.leaderFlag == "1" || (row.owner != undefined && row.owner != "")) { //组长
                                    return true;
                                } else {
                                    return false;
                                }
                            }
                        }
                    }
                ]
            }]
    };

    //选择办理人
    $scope.selectEmp = function(emp,event) {
        if($(event.currentTarget)[0].checked) { //选中
            $scope.selectEmpList.push(emp);
        } else { //取消
            for (var i = 0; i < $scope.selectEmpList.length; i++) {
                var obj = $scope.selectEmpList[i];
                if(obj.empId == emp.empId) {
                    $scope.selectEmpList.splice(i,1);
                }
            }
        }
    };

    $scope.selectable = function(data) {
        if(data.id.indexOf("_") != -1) { //选择的部门
            return false;
        }
    };

    $scope.completeModalObj = {
        //完成
        ok : function() {
            if(!$scope.hasOwnerFlag) {
                if($scope.selectEmpList == undefined || $scope.selectEmpList.length == 0) {
                    Page.notice("请选择办理人!");
                    return;
                }
            }
            Page.ajaxPost('maintenanceApply/mtComplete.do',{taskId : $scope.completeTask.taskId,hasOwnerFlag : $scope.hasOwnerFlag,
                repairEmpId : $scope.completeTask.id,
                empList : $scope.selectEmpList, comment : $scope.completeTask.comment,files : $scope.completeTask.images},function(){
                Page.notice("操作成功!");
                $scope.completeModal.hide();
                $scope.grid.query();
            });
        }
    };

    $scope.cancelCompleteTaskModal = function() {
        $scope.completeModal.hide();
    };




    //删除上传文件
    $scope.removeImg = function(img) {
        for (var i = 0; i < $scope.completeTask.images.length; i++) {
            var obj = $scope.completeTask.images[i];
            if(img.name == obj.name && img.path == obj.path) {
                $scope.completeTask.images.splice(i,1);
                break;
            }
        }
    }

}]);